package com.briup.cms.web.controller;

import com.briup.cms.bean.BaseUser;
import com.briup.cms.service.IBaseUserService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import com.briup.cms.vm.UpdateUserVM;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户控制器类
 **/
@Api(tags="用户相关接口")
@RestController
@RequestMapping("/auth/user")
public class UserController {

    @Autowired
    private IBaseUserService baseUserService;

    @ApiOperation("获取当前登录用户的基本信息")
    @GetMapping("/info")
    public Message info(@ApiIgnore @RequestHeader("Authorization") String token){
        BaseUser user = baseUserService.currentUserInfo(token);
        return MessageUtil.success(user);
    }

    @ApiOperation("根据用户名角色、名分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名", paramType = "query"),
            @ApiImplicitParam(name = "role", value = "角色名,0-admin,1-user", paramType = "query",allowableValues = "0,1")
    })
    @GetMapping("/page/query")
    public Message findUsersWithPage(int pageNum, int pageSize, String username,Integer role){
        PageInfo<BaseUser> data = baseUserService.pageQuery(pageNum, pageSize, username, role);
        return MessageUtil.success(data);
    }


    @ApiOperation(value = "更新用户信息",notes = "能更新的字段：telephone realname gender dob email")
    @PutMapping("/update")
    public Message updateUser(UpdateUserVM updateUserVM){
        baseUserService.updateUser(updateUserVM);
        return MessageUtil.success("更新成功");
    }


    @ApiOperation("修改用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userIcon", value = "用户头像", required = true, paramType = "query"),
    })
    @PutMapping("/updateUserIcon")
    public Message updateUserIcon(int id,String userIcon){
        baseUserService.updateUserIcon(id,userIcon);
        return MessageUtil.success("修改用户头像成功");
    }

    @ApiOperation("通过id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query"),
    })
    @DeleteMapping("/delete")
    public Message deleteById(int id){
        baseUserService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

}
